package com.dykj.fireFighting.web.controller;

import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.monitoringCenter.entity.CityDevice;
import com.dykj.fireFighting.api.monitoringCenter.service.ICityDeviceService;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/monitor/cityDevice")
public class CityDeviceController {

    @DubboReference
    private ICityDeviceService iCityDeviceService;
    @DubboReference
    private IEnterpriseService iEnterpriseService;

    //获取列表数据
    @ApiOperation(value = "获取市政设备列表", notes = "获取市政设备列表")
    @GetMapping("/list")
    public CommonResult<List<CityDevice>> getList(@RequestParam(required = false) String unitId, @RequestParam(required = false) String name) {
        if (StringUtils.isBlank(unitId)) {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            if (loginUserVo.getUserType() != 0 && loginUserVo.getEnterpriseType() == 2) {
                unitId = String.valueOf(loginUserVo.getEnterpriseId());
            }
        }
        List<CityDevice> list = iCityDeviceService.getList(unitId, name);
        return CommonResult.success(list);
    }

    //获取列表数据
    @ApiOperation(value = "获取市政设备信息", notes = "获取市政设备信息")
    @GetMapping("/info/{id}")
    public CommonResult<CityDevice> getInfo(@PathVariable(value = "id") String id) {
        return CommonResult.success(iCityDeviceService.getById(id));
    }

    @ApiOperation(value = "添加市政设备", notes = "添加市政设备")
    @PostMapping("/add")
    public CommonResult save(@Valid @RequestBody CityDevice cityDevice) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        if (loginUserVo.getUserType() != 0 && loginUserVo.getEnterpriseType() == 2) {
            String unitId = String.valueOf(loginUserVo.getEnterpriseId());
            cityDevice.setUnitId(unitId);
        } else {
            cityDevice.setUnitId("-1");
        }
        cityDevice.setCreateTime(new Date());
        cityDevice.setIsDel(0);
        cityDevice.setId(UUIDUtils.get32UUID());

        iCityDeviceService.save(cityDevice);
        return CommonResult.success("success");
    }


    @ApiOperation(value = "修改市政设备", notes = "修改市政设备")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody CityDevice cityDevice) {

        cityDevice.setUpdateTime(new Date());
        cityDevice.setIsDel(0);

        iCityDeviceService.updateById(cityDevice);
        return CommonResult.success("success");
    }

    @ApiOperation(value = "删除市政设备", notes = "删除市政设备")
    @GetMapping("/delete")
    public CommonResult update(String id) {
        iCityDeviceService.deleteById(id);
        return CommonResult.success("success");
    }

    @ApiOperation(value = "获取监控中心下的社会单位信息", notes = "获取监控中心下的社会单位信息")
    @GetMapping("/getSocialList")
    public CommonResult getSocialList(String id) {
        if (StringUtils.isBlank(id)) {
            id = String.valueOf(ShiroUtil.getUser().getEnterpriseId());
        }
        return CommonResult.success(iEnterpriseService.queryUnitSelect(id), "success");
    }

}
